package com.lagou.edu.annotation;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.ModelAndView;


import java.util.Arrays;
import java.util.Date;
import java.util.List;


@Aspect
@Component
public class SecurityAspect {

    private static final org.slf4j.Logger log = LoggerFactory.getLogger(SecurityAspect.class);

//    @Autowired
//    private SysObsSpdService sysObsSpdService;
//
//    @Autowired
//    private RedissonClient redissonClient;

    // 修正Timer注解的全局唯一限定符
    @Pointcut("@annotation(com.lagou.edu.annotation.Security)")
    private void SecurityAspect() {
    }

    @Around("SecurityAspect()")
    public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {

        // 获取目标类名称
        String clazzName = proceedingJoinPoint.getTarget().getClass().getName();

        // 获取目标类方法名称
        String methodName = proceedingJoinPoint.getSignature().getName();

        // 获取传入参数
        Object[] argsArrayInit = proceedingJoinPoint.getArgs();

        Security security = getSecurityInfo(proceedingJoinPoint);
        String userNamesSet= getUserNames(proceedingJoinPoint, security);

        String userName = (String) argsArrayInit[0];

        String[] userNameArray = userNamesSet.split(",");
        List<String> userNameList = Arrays.asList(userNameArray);

        if(userNameList.contains(userName)){
            Object result = proceedingJoinPoint.proceed();
            System.out.println("用户名合法");
            return result;
        } else{
            Date date = new Date();// 服务器时间
            // 返回服务器时间到前端页面
            // 封装了数据和页面信息的 ModelAndView
            ModelAndView modelAndView = new ModelAndView();
            // addObject 其实是向请求域中request.setAttribute("date",date);
            modelAndView.addObject("msg", "用户名不合法");
            // 视图信息(封装跳转的页面信息) 逻辑视图名
            modelAndView.setViewName("error");
            return (Object) modelAndView;
        }

    }

    public Security getSecurityInfo(ProceedingJoinPoint proceedingJoinPoint) {
        MethodSignature methodSignature = (MethodSignature) proceedingJoinPoint.getSignature();
        return methodSignature.getMethod().getAnnotation(Security.class);
    }

    /**
     * @param proceedingJoinPoint
     * @return
     */
    public String getUserNames(ProceedingJoinPoint proceedingJoinPoint, Security security) {
        StringBuilder userNames = new StringBuilder();
        final Object[] args = proceedingJoinPoint.getArgs();
        MethodSignature methodSignature = (MethodSignature) proceedingJoinPoint.getSignature();
        userNames.append(security.userNames());

        return userNames.toString();
    }


}
